package com.snb.play.dwebtoon.data;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.snb.play.dwebtoon.IntroActivity;
import com.snb.play.dwebtoon.util.Logger;

public class DTO_WebToon extends JSonDataObjectBase {

	/**
	 * 
	 */
	public static final long serialVersionUID = 101L;

	public String lastTouch = "";
	// public String viewerType = "";
	public String title = "";

	// //
	// public String startDate = "";
	public String averageScore = "";
	// public DTO_Cartoon cartoon = "";
	// public DTO_Cartoon cartoon = "";start
	public String picture_artists = "";
	public String write_artists = "";
	public String picture_artists_id = "";
	public String write_artists_id = "";
	// public ArrayList<DTO_Artist> artists = "";
	public String genres = "";
	// public ArrayList<DTO_Genre> genres = "";
	// public DTO_Cartoon cartoon = ""; end

	// public String diff = "";

	// public String englishTitle = "";
	// public String seriesYn = "";
	// public String tag = "";
	public String score = "";
	public String webtoonWeeks = "";

	public String seriesWebtoons = "";
	public String articleId = "";
	// public String payYn = "";
	// public String commentUseYn = "";
	public String id = "";

	// public String event = "";
	// public String webtoonComment = "";
	// public String titleHtml = "";
	// public String hasPadtoon = "";
	// public String viewerEvent = "";
	// public String webtoonType = "";
	public String finishYn = "";
	public String savedYn = "";
	// public String weekTerm = "";
	public String nickname = "";
	// public String isNew = "";
	// public String padtoonYn = "";
	// public String webtoonGroupId = "";
	// public String finishDate = "";
	// public String webtoonEpisodes = "";
	// public String url = "";
	public String price = "";
	// public String serviceStatus = "";

	// public String webtoonServices = "";
	// public String latestWebtoonEpisode = "";
	// public String dateCreated = "";
	// public String ageGrade = "";
	// public String ranking = "";
	// public String mobileYn = "";
	// public String media = "";
	// public String padYn = "";
	// public String introduction = "";

	// public DTO_PictureImage titleImage = "";
	// public DTO_PictureImage titleImage2 = "";
	// public DTO_PictureImage thumbnailImage = "";
	public String thumbnailImage = "";

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getLastTouch() {
		return lastTouch;
	}

	public String getTitle() {
		return title;
	}

	public String getAverageScore() {
		return averageScore;
	}

	public String getPicture_artists() {
		return picture_artists;
	}

	public String getWrite_artists() {
		return write_artists;
	}

	public String getGenres() {
		return genres;
	}

	public String getScore() {
		return score;
	}

	public String getWebtoonWeeks() {
		return webtoonWeeks;
	}

	public String getSeriesWebtoons() {
		return seriesWebtoons;
	}

	public String getArticleId() {
		return articleId;
	}

	public String getId() {
		return id;
	}

	public String getFinishYn() {
		return finishYn;
	}

	public String getNickname() {
		return nickname;
	}

	public String getPrice() {
		return price;
	}

	public String getThumbnailImage() {
		return thumbnailImage;
	}

	public String getThumbnailImage2() {
		return thumbnailImage2;
	}

	public static String getTAG() {
		return TAG;
	}

	public void setLastTouch(String lastTouch) {
		this.lastTouch = lastTouch;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setAverageScore(String averageScore) {
		this.averageScore = averageScore;
	}

	public void setPicture_artists(String picture_artists) {
		this.picture_artists = picture_artists;
	}

	public void setWrite_artists(String write_artists) {
		this.write_artists = write_artists;
	}

	public void setGenres(String genres) {
		this.genres = genres;
	}

	public void setScore(String score) {
		this.score = score;
	}

	public void setWebtoonWeeks(String webtoonWeeks) {
		this.webtoonWeeks = webtoonWeeks;
	}

	public String getPicture_artists_id() {
		return picture_artists_id;
	}

	public String getWrite_artists_id() {
		return write_artists_id;
	}

	public void setPicture_artists_id(String picture_artists_id) {
		this.picture_artists_id = picture_artists_id;
	}

	public void setWrite_artists_id(String write_artists_id) {
		this.write_artists_id = write_artists_id;
	}

	public void setSeriesWebtoons(String seriesWebtoons) {
		this.seriesWebtoons = seriesWebtoons;
	}

	public void setArticleId(String articleId) {
		this.articleId = articleId;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setFinishYn(String finishYn) {
		this.finishYn = finishYn;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public void setPrice(String price) {
		this.price = price;
	}

	public void setThumbnailImage(String thumbnailImage) {
		this.thumbnailImage = thumbnailImage;
	}

	public void setThumbnailImage2(String thumbnailImage2) {
		this.thumbnailImage2 = thumbnailImage2;
	}

	public static void setTAG(String tAG) {
		TAG = tAG;
	}

	// public DTO_PictureImage thumbnailImage2;
	public String thumbnailImage2;
	// public DTO_PictureImage padThumbnailImage;
	// public DTO_PictureImage artistCommentImage;
	// public DTO_PictureImage homeThumbnailImage;

	// public DTO_PictureImage padThumbnailImage;
	// public DTO_PictureImage artistCommentImage;
	// public DTO_PictureImage thumbnailImage;
	// public DTO_PictureImage thumbnailImage2;

	// ///
	public static String TAG = DTO_WebToon.class.getSimpleName();

	public DTO_WebToon() {
		super();
	}

	public DTO_WebToon(JSONObject aJsonObject) {
		@SuppressWarnings("unchecked")
		Iterator<Object> iterator = aJsonObject.keys();

		while (iterator.hasNext()) {
			Object obj = iterator.next();
			if (obj.toString().equalsIgnoreCase("cartoon")) {
				try {
					JSONObject lNew = aJsonObject.getJSONObject("cartoon");
					if (lNew.has("artists")) {
						Logger.d(TAG, "artists");
						DTO_Artist lArtist = null;
						try {
							JSONArray lartists = lNew.getJSONArray("artists");

							for (int i = 0; i < lartists.length(); i++) {
								lArtist = new DTO_Artist(
										lartists.getJSONObject(i));
								if (lArtist.artistType.indexOf("picture") > -1) {
									this.picture_artists = lArtist.penName;
									this.picture_artists_id = lArtist.id;
								} else {
									this.write_artists = lArtist.penName;
									this.write_artists_id = lArtist.id;
								}
							}
						} catch (JSONException e) {
							if (isDebug)
								e.printStackTrace();
						}

					}
					if (lNew.has("genres")) {
						Logger.d(TAG, "genres");
						this.genres = "";
						DTO_Genre lGenre = null;
						try {
							JSONArray lgenres = lNew.getJSONArray("genres");

							for (int i = 0; i < lgenres.length(); i++) {
								lGenre = new DTO_Genre(lgenres.getJSONObject(i));
								// this.genres.add(lGenre);
								this.genres += lGenre.name;
							}
						} catch (JSONException e) {
							if (isDebug)
								e.printStackTrace();
						}

					}
				} catch (Exception e) {
					// TODO: handle exception
				}
			} else if (obj.toString().equalsIgnoreCase("titleImage")) {
			} else if (obj.toString().equalsIgnoreCase("titleImage2")) {
			} else if (obj.toString().equalsIgnoreCase("thumbnailImage")) {
				try {
					this.thumbnailImage = aJsonObject.getJSONObject(
							"thumbnailImage").getString("url");
				} catch (JSONException e) {
					if (isDebug)
						e.printStackTrace();
				}
			} else if (obj.toString().equalsIgnoreCase("thumbnailImage2")) {
				try {
					this.thumbnailImage2 = aJsonObject.getJSONObject(
							"thumbnailImage2").getString("url");
				} catch (JSONException e) {
					if (isDebug)
						e.printStackTrace();
				}
			} else if (obj.toString().equalsIgnoreCase("padThumbnailImage")) {
			} else if (obj.toString().equalsIgnoreCase("artistCommentImage")) {
			} else if (obj.toString().equalsIgnoreCase("homeThumbnailImage")) {
			} else if (obj.toString().equalsIgnoreCase("webtoonWeeks")) {
			} else {
				setMemberFiled(this, aJsonObject, obj.toString());
			}
		}

		setUpdateWeek(aJsonObject);
		this.savedYn = "N";

		if (this.lastTouch == null || this.lastTouch.length() < 1) {
			this.lastTouch = IntroActivity.gTimeStamp;
		}

		printMembers();

	}

	public void setUpdateWeek(JSONObject aJsonObject) {
		if (!this.finishYn.equalsIgnoreCase("y")) {
			JSONArray nA = null;
			this.webtoonWeeks = "";
			try {
				nA = aJsonObject.getJSONArray("webtoonWeeks");
				Logger.d(TAG, "webtoonWeeks");
				if (nA != null && nA.length() > 0) {
					for (int i = 0; i < nA.length(); i++) {
						JSONObject lT = (JSONObject) nA.get(i);
						this.webtoonWeeks += lT.getString("weekDay") + "|";
					}
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	public void setRead(Context aCtx) {
		Map<String, Object> lMap = new HashMap<String, Object>();
		lMap.put("lastTouch", this.lastTouch);
		updateTouch(aCtx, lMap);
	}

	public void setSave(Context aCtx) {
		Map<String, Object> lMap = new HashMap<String, Object>();
		lMap.put("savedYn", this.savedYn);
		updateTouch(aCtx, lMap);
	}

}
